package com.jzo2o.customer.service.impl;


import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务人员认证审核表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-03
 */
@Service
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements IWorkerCertificationAuditService {
    @Autowired
    private IWorkerCertificationService workerCertificationService;


    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult pageQuery(WorkerCertificationAuditPageQueryReqDTO dto) {

        //判断参数
        if (ObjectUtils.isEmpty(dto)) {
            throw new BadRequestException("参数错误");
        }
        //根据条件查询
        LambdaQueryChainWrapper<WorkerCertificationAudit> wrapper = this.lambdaQuery();
        //根据审核状态查找
        Page<WorkerCertificationAudit> page = wrapper
                .eq(ObjectUtils.isNotEmpty(dto.getIdCardNo()), WorkerCertificationAudit::getIdCardNo, dto.getIdCardNo())
                .like(ObjectUtils.isNotEmpty(dto.getName()), WorkerCertificationAudit::getName, dto.getName())
                .eq(ObjectUtils.isNotEmpty(dto.getAuditStatus()), WorkerCertificationAudit::getAuditStatus, dto.getAuditStatus())
                .eq(ObjectUtils.isNotEmpty(dto.getCertificationStatus()), WorkerCertificationAudit::getCertificationStatus, dto.getCertificationStatus())
                .orderBy(ObjectUtils.isNotEmpty(dto.getOrderBy1()), dto.getIsAsc1(), WorkerCertificationAudit::getCreateTime)
                .orderBy(ObjectUtils.isNotEmpty(dto.getOrderBy2()), dto.getIsAsc2(), WorkerCertificationAudit::getAuditTime)
                .page(new Page<>(dto.getPageNo(), dto.getPageSize()));

        return PageUtils.toPage(page, WorkerCertificationAudit.class);

    }

    /**
     * 审核服务人员认证信息
     *
     * @param id
     * @param certificationStatus
     * @param rejectReason
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void audit(Long id, Integer certificationStatus, String rejectReason) {
        //判断参数
        if (ObjectUtils.isEmpty(id) || ObjectUtils.isEmpty(certificationStatus)){
            throw new BadRequestException("参数错误");
        }
        //在audit表根据id找到认证信息
        WorkerCertificationAudit audit = this.lambdaQuery().eq(WorkerCertificationAudit::getId, id).one();
        if (ObjectUtils.isEmpty(audit)){
            throw new BadRequestException("认证信息不存在");
        }
        //修改公共信息
        //已审核
        audit.setAuditStatus(1);
        //审核时间
        audit.setAuditTime(LocalDateTime.now());
        //审核人id,姓名
        audit.setAuditorId(UserContext.currentUserId());
        audit.setAuditorName(UserContext.currentUser().getName());


        //在worker表根据id找到认证信息
        WorkerCertification certification = workerCertificationService.lambdaQuery().eq(WorkerCertification::getId, audit.getServeProviderId()).one();
        if (ObjectUtils.isEmpty(certification)){
            throw new BadRequestException("认证信息不存在");
        }
        //修改认证时间
        certification.setCertificationTime(LocalDateTime.now());


        //如果是认证通过
        if (certificationStatus == 2){
            //audit认证通过
            audit.setCertificationStatus(2);

            //修改worker表认证状态
            certification.setCertificationStatus(2);

        }else if (certificationStatus == 3){
            //audit认证失败,同时封装驳回原因
            audit.setCertificationStatus(3);
            audit.setRejectReason(rejectReason);

            //修改worker表认证状态
            certification.setCertificationStatus(3);
        }
        //保存修改
        boolean b = this.updateById(audit);
        boolean b1 = workerCertificationService.updateById(certification);
        if (!b || !b1){
            throw new BadRequestException("审核操作失败");
        }

    }
}
